home *** CD-ROM | disk | FTP | other *** search
- Path: news.halcyon.com!usenet
- From: normanb@halcyon.com (Norm Bryar)
- Newsgroups: comp.lang.c++
- Subject: Re: Help: Bitwise operators
- Date: Sat, 13 Apr 1996 16:32:10 GMT
- Organization: Northwest Nexus Inc.
- Message-ID: <4koksu$fai@news.halcyon.com>
- References: <4knb73$f15@wumpus.its.uow.edu.au>
- NNTP-Posting-Host: blv-pm3-ip22.halcyon.com
- X-Newsreader: Forte Free Agent 1.0.82
-
- sjl01@wumpus.its.uow.edu.au (Stuart John Langley) wrote:
-
- >Hi All,
-
- >I need some help understanding some code that i need to translate.
- >I have the following,
-
- >unsigned char byte;
-
- >then either -
-
- >byte = (byte >> 1) | 0;
-
- >or
-
- >byte = (byte >> 1) | 128;
-
- >I know that the first case places a 0 in the left most bit of byte,
- >and moves everything else to the right, while the second places a 1
- >in the leftmost bit. However, when I look at the texts and hepfiles,
- >this seems to indicate thet the 0 or 1 should be placed in the rightmost
- >bit and everything else moved left. Can somebody please help me out with
- >this problem>
-
- >Thanks,
-
- >Stuart
-
- What exactly do the docs say? What is happening is the byte is
- right-shifted by one, and that result bitwise-ORd with the given
- value.
-
- In the first case the bitwise-OR of zero does absolutely nothing.
- The shift itself made the zero in the most-significant (leftmost) bit.
- (This only holds true if byte is of type unsigned char; for normal,
- signed char types, shifting preserves the sign-bit, the leftmost bit.
- In this way, shifting right by 1 is always equivalent to / 2).
-
- In the second case, the | 128 is really | 0x80, thus setting the
- leftmost bit after the shift.
-
- --Norm
-
-